package com.example.teamtribe.main.club;

import android.os.Bundle;

import com.example.teamtribe.common.Back;
import com.example.teamtribe.common.CallbackStore;
import com.example.teamtribe.common.model.FileModel;
import com.example.teamtribe.database.Team;
import com.example.teamtribe.json.JsonTeam;
import com.example.teamtribe.utils.HttpUtil;
import com.example.teamtribe.utils.URLStore;
import com.google.gson.Gson;

import org.litepal.crud.DataSupport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ClubModel {

    /***
     *
     * @param schoolId 0不限制、1长沙理工大学
     * @param tagId 0不限制、1其他、2创新创业团队、3社会实践社团、4文学艺术社团、5科技艺术社团、6体育运动社团
     */
    public void getTeamIds(int schoolId, int tagId, Back back) {
        Map<String, String> params = new HashMap<>(2);
        params.put("schoolId", schoolId + "");
        params.put("tagId", tagId + "");
        HttpUtil.post(URLStore.TEAM_GET, params, CallbackStore.getIdsCallback(back));
    }

    public void getTeam(int teamId, FileModel.FileBack back) {
        Map<String, String> params = new HashMap<>(1);
        params.put("id", teamId + "");
        HttpUtil.post(URLStore.TEAM_INFO_GET, params, new CallbackStore.CallbackImpl<FileModel.FileBack>(back) {
            @Override
            public void onResponse(String json) {
                JsonTeam jsonTeam = new Gson().fromJson(json, JsonTeam.class);
                Team team = jsonTeam.getTeam();
                Bundle bundle = new Bundle();
                bundle.putParcelable("team", team);
                back.success(bundle);
                FileModel.getInstance().download(team.getPortrait(), 0, back);
            }
        });
    }

    public List<Team> getLocalTeams(int schoolId) {
        return DataSupport.where("schoolId = ? ", schoolId + "").find(Team.class);
    }

    public void clearLocalTeams() {
        DataSupport.deleteAll(Team.class);
    }
}
